﻿//********************************************************************************//
//创建日期：<创建日期，2016-10-21>
//创建作者：<郭登杰，guodengjie@shanghai3h.com>
//功能说明：来涵井信息
//********************************************************************************//
using BLL;
using BLL.Access;
using Entity;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Web.Class;
using Web.Controls.LAIHAN;

namespace Web.Controls
{
    public partial class UCLHJinInfoQS : System.Web.UI.UserControl, IUCLHOperate
    {
        #region 属性

        /// <summary>
        /// 来函类型
        /// </summary>
        public eLaiHanLX LaiHanLX
        {
            get;
            set;
        }

        /// <summary>
        /// 当前流程
        /// </summary>
        public eLaiHanLCBH LaiHanLC
        {
            get;
            set;
        }

        /// <summary>
        /// 井编号
        /// </summary>
        public string JINBH
        {
            set
            {
                this.txtJinBH.Text = value;
            }
            get
            {
                return this.txtJinBH.Text;
            }
        }

        /// <summary>
        /// 用户名
        /// </summary>
        public string YONGHUM
        {
            set
            {
                this.txtYongHuM.Text = value;
            }
            get
            {
                return this.txtYongHuM.Text;
            }
        }

        /// <summary>
        /// 地址
        /// </summary>
        public string DIZHI
        {
            set
            {
                this.txtDiZhi.Text = value;
            }
            get
            {
                return this.txtDiZhi.Text;
            }
        }

        /// <summary>
        /// 来函编号
        /// </summary>
        public string LAIHANBH
        {
            set
            {
                this.txtLaiHanBH.Text = value;
            }
            get
            {
                return this.txtLaiHanBH.Text;
            }
        }


        /// <summary>
        /// 判断引用页面类别   1月调整登记  2月调整修改  3其他页面调用 不可修改
        /// </summary>
        public int ControlType
        {
            get { return Convert.ToInt32(ViewState["ControlType"]); }
            set { ViewState["ControlType"] = value; }
        }

        /// <summary>
        /// 记录错误日志
        /// </summary>
        private string errorMsg
        {
            get
            {
                return Convert.ToString(ViewState["errorMsg"]);
            }
            set
            {
                ViewState["errorMsg"] = value;
            }
        }

        #endregion

        #region 变量

        #endregion

        #region 事件

        /// <summary>
        /// 加载事件
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitControl();
            }
        }

        /// <summary>
        /// 回调响应
        /// </summary>
        protected void ASPxCallback1_Callback(object source, DevExpress.Web.CallbackEventArgs e)
        {
            CallbackResult cr = new CallbackResult(e.Parameter);
            if (e.Parameter == "bh")//响应用户号回车事件
            {
                DataRowCollection drc = null;
                if (this.LaiHanLX == eLaiHanLX.水资源销取水口 || this.LaiHanLX == eLaiHanLX.水资源取水口过户)
                {
                    drc = new SpHelper().P_QSLH_QSKYHXXHQ(null, (int)this.LaiHanLX, this.txtJinBH.Text).Tables[0].Rows;
                }
                else if (this.LaiHanLX == eLaiHanLX.水资源销井 || this.LaiHanLX == eLaiHanLX.水资源井过户)
                {
                    drc = new QS_YH_SHENJINGXXBProcess().SelectQS_YH_SHENJINGXXBsDynamic("S_JINGBH='" + this.txtJinBH.Text + "'", null).Rows;
                    //drc = new SpHelper().P_QS_JINYHXXHQ(null,this.txtJinBH.Text,(int)this.LaiHanLX).Tables[0].Rows;
                }
                if (drc.Count == 0)
                {
                    cr.IsSuccess = false;
                    cr.IsAlertMsg = false;
                    if (this.LaiHanLX == eLaiHanLX.水资源销井 || this.LaiHanLX == eLaiHanLX.水资源井过户)
                    {
                        cr.Msg = "查询不到该井信息。";
                    }
                    else if (this.LaiHanLX == eLaiHanLX.水资源销取水口 || this.LaiHanLX == eLaiHanLX.水资源取水口过户)
                    {
                        cr.Msg = "查询不到该取水口信息";
                    }
                    cr.IsAlertMsg = true;
                    e.Result = cr.ToString();
                    return;
                }
                else
                {
                    int type=-1;
                    if (this.LaiHanLX == eLaiHanLX.水资源销井 || this.LaiHanLX == eLaiHanLX.水资源井过户)
                    {
                        type = 0;
                        if (Convert.ToInt32(drc[0]["S_JINGZHUANGTBH"]) == (int)eJinXZ.注销许可证 || Convert.ToString(drc[0]["S_JIESHUNY"]) != "")
                        {
                            cr.IsSuccess = false;
                            cr.IsAlertMsg = true;
                            cr.Msg = "该井目前处于注销状态。";
                            e.Result = cr.ToString();
                            return;
                        }
                    }
                    else if (this.LaiHanLX == eLaiHanLX.水资源销取水口 || this.LaiHanLX == eLaiHanLX.水资源取水口过户)
                    {
                        type = 1;
                    }
                    //获取对应用户信息                    
                    DataRowCollection drcyhxx = new SpHelper().P_QS_GUANLIANYHXXGET(type, BusinessTool.GetWaterYM(), BusinessTool.WType, this.txtJinBH.Text).Tables[0].Rows;
                    string yhh, yhm;
                    if (drcyhxx.Count == 0)
                    {
                        yhh = yhm = "";
                    }
                    else
                    {
                        yhh = Convert.ToString(drcyhxx[0]["S_YONGHUH"]);
                        yhm = Convert.ToString(drcyhxx[0]["S_YONGHUM"]);
                    }
                    if (type == 0)
                    {
                        cr.AddValue(Convert.ToString(drc[0]["S_JINGDIZ"]), Convert.ToString(drc[0]["S_LAOBIANH"]), yhh, yhm);
                    }
                    else if (type == 1)
                    {
                        cr.AddValue(Convert.ToString(drc[0]["S_DIZHI"]), Convert.ToString(drc[0]["S_BHY"]), yhh, yhm);
                    }
                    cr.IsAlertMsg = false;
                    cr.IsSuccess = true;
                    e.Result = cr.ToString();
                }
            }
            else if (e.Parameter == "bh2")//响应目标用户号回车事件
            {
                DataRowCollection drc = new QS_YG_YONGHUXXBLL().SelectQS_YG_YONGHUXXesDynamic("I_JLZT=0 and I_WTYPE=" + BusinessTool.WType + " and S_YONGHUH='" + this.txtTargetYongHuH.Text + "'", null).Rows;
                if (drc.Count == 0)
                {
                    cr.IsSuccess = false;
                    cr.IsAlertMsg = false;
                    cr.Msg = "无此用户。";
                    cr.IsAlertMsg = true;
                    e.Result = cr.ToString();
                    return;
                }
                else
                {
                    cr.AddValue(Convert.ToString(drc[0]["S_YONGHUM"]));
                    cr.IsAlertMsg = false;
                    cr.IsSuccess = true;
                    e.Result = cr.ToString();
                }
            }
        }

        /// <summary>
        /// 前台验证后实际执行后台操作
        /// </summary>
        protected void btnActPerform_Click(object sender, EventArgs e)
        {
            YS_LH_LAIHANXX laihanxx = new YS_LH_LAIHANXX();
            laihanxx.S_LAIHANBH = null;
            laihanxx.I_LAIHANLX = (int)this.LaiHanLX;
            laihanxx.I_LIUCHENGBH = (int)eLaiHanLCBH.未登记;
            laihanxx.S_YONGHUBH = this.txtYongHuH.Text;
            laihanxx.S_BEIZHU = this.txtJinBH.Text;
            IUCLHOperate ucinfo = null;
            if (this.LaiHanLX == eLaiHanLX.水资源销井)
            {
                ucinfo = this.Parent.FindControl("UCLHShuiBiaoListQS") as IUCLHOperate;
                if (ucinfo != null)
                {
                    ucinfo.LoadInfo(laihanxx);
                }
                //ucinfo = this.Parent.FindControl("UCLHJinListQS") as UCLHJinListQS;
                //if (ucinfo != null)
                //{
                //    ucinfo.LoadInfo(laihanxx);
                //}
            }
            else if ( this.LaiHanLX == eLaiHanLX.水资源井过户)
            {
                ucinfo = this.Parent.FindControl("UCLHJinListQS") as UCLHJinListQS;
                if (ucinfo != null)
                {
                    ((UCLHJinListQS)ucinfo).RegistMemberValue(new string[] { this.txtYongHuH.Text, this.txtTargetYongHuH.Text });
                    ucinfo.LoadInfo(laihanxx);
                }
            }
            else if (this.LaiHanLX == eLaiHanLX.水资源销取水口 || this.LaiHanLX == eLaiHanLX.水资源取水口过户)
            {
                ucinfo = this.Parent.FindControl("UCLHShuiBiaoListQSK") as UCLHShuiBiaoListQSK;
                ucinfo.LoadInfo(laihanxx);
                if (this.LaiHanLX == eLaiHanLX.水资源取水口过户)
                {
                    ((UCLHShuiBiaoListQSK)ucinfo).RegistTagetYongHu(this.txtTargetYongHuH.Text, this.txtTargetYongHuM.Text);
                }
            }


        }

        #endregion

        #region 方法

        /// <summary>
        /// 配置控件
        /// </summary>
        public void InitControl()
        {
            //默认设置错误信息,防止用户不搜索直接提交
            this.errorMsg = "error";
            this.txtDiZhi.ClientEnabled = false;
            this.txtJinBH.ClientEnabled = true;
            this.txtLaiHanBH.ClientEnabled = false;
            this.txtXuKeZH.ClientEnabled = false;
            this.txtLaoBH.ClientEnabled = false;
            this.txtYongHuH.ClientEnabled = false;
            this.txtYongHuM.ClientEnabled = false;
            this.txtTargetYongHuH.ClientEnabled = true;
            this.txtTargetYongHuM.ClientEnabled = false;
            this.trYHDZ.Visible = false;
            this.trTarget1.Visible = (this.LaiHanLX == eLaiHanLX.水资源井过户 || this.LaiHanLX == eLaiHanLX.水资源取水口过户);
            this.trTarget2.Visible = (this.LaiHanLX == eLaiHanLX.水资源井过户 || this.LaiHanLX == eLaiHanLX.水资源取水口过户);
            if (this.LaiHanLX == eLaiHanLX.水资源取水口过户 || this.LaiHanLX == eLaiHanLX.水资源销取水口)
            {
                this.lblTitle.Text = "取水口信息";
                this.lblBH.Text = "取水口编号：";
                this.lblYBH.Text = "原水口编号：";
                this.lblDZ.Text = "取水口地址：";
            }
            else
            {
                this.lblTitle.Text = "井信息";
                this.lblBH.Text = "井编号：";
                this.lblYBH.Text = "原井编号：";
                this.lblDZ.Text = "井地址：";
            }
            this.txtJinBH.Focus();

        }

        /// <summary>
        ///根据当前流程状态设置步骤显示
        /// </summary>
        private void SetDisplay()
        {
            //根据类型显示需要的             
            //受理之后不可编辑.下方switch中case 3可去除.使用此控件的受理之后的页面该控件属性需加上LaiHanLC="受理"
            if ((int)this.LaiHanLC <= (int)eLaiHanLCBH.预登记)
            {
                this.txtJinBH.ClientEnabled = true;
                this.txtTargetYongHuH.ClientEnabled = true;
            }
            if ((int)this.LaiHanLC >= (int)eLaiHanLCBH.登记)
            {
                this.txtJinBH.ClientEnabled = false;
                this.txtTargetYongHuH.ClientEnabled = false;
                //this.btnChaXun.ClientEnabled = false;
            }
        }

        /// <summary>
        /// 验证数据
        /// </summary>
        public ValidateRS Validate()
        {
            ValidateRS rs = new ValidateRS();
            if (this.txtJinBH.ClientEnabled)
            {
                if (this.txtDiZhi.Text == "" & this.txtLaoBH.Text == "" & this.txtTargetYongHuH.Text == ""
                    & this.txtTargetYongHuM.Text == "" & this.txtYongHuH.Text == "" & this.txtYongHuM.Text == "")
                {
                    rs.Msg = "请将登记信息填写完整。";
                    return rs;
                }
            }
            rs.IsSuccess = true;
            return rs;
        }

        /// <summary>
        /// 清除数据
        /// </summary>
        public void ClearInfo()
        {
            this.txtDiZhi.Text = "";
            this.txtJinBH.Text = "";
            this.txtLaiHanBH.Text = "";
            this.txtXuKeZH.Text = "";
            this.txtLaoBH.Text = "";
            this.txtYongHuH.Text = "";
            this.txtYongHuM.Text = "";
            this.txtTargetYongHuH.Text = "";
            this.txtTargetYongHuM.Text = "";
        }


        /// <summary>
        /// 根据来函加载信息
        /// </summary>
        public void LoadInfo(YS_LH_LAIHANXX laihanxx)
        {
            //登记前
            if (laihanxx.ID == 0)
            {
                this.LaiHanLC = eLaiHanLCBH.未登记;
                SetDisplay();
                return;
            }
            else
            {
                this.LaiHanLC = (eLaiHanLCBH)Enum.Parse(typeof(eLaiHanLCBH), laihanxx.I_LIUCHENGBH.ToString());
                SetDisplay();
            }
            //加载许可号等信息
            this.LAIHANBH = laihanxx.S_LAIHANBH;
            this.txtXuKeZH.Text = laihanxx.S_XUKEBH;
            if (this.LaiHanLX == eLaiHanLX.水资源销井 || this.LaiHanLX == eLaiHanLX.水资源注销井恢复 || this.LaiHanLX == eLaiHanLX.水资源井过户)
            {
                //加载井信息
                DataRow dr = new SpHelper().P_QS_JINYHXXHQ(laihanxx.S_LAIHANBH, laihanxx.S_BEIZHU, (int)this.LaiHanLX).Tables[0].Rows[0];
                this.txtJinBH.Text = laihanxx.S_BEIZHU;
                this.txtDiZhi.Text = Convert.ToString(dr["S_JINGDIZ"]);
                this.txtLaiHanBH.Text = laihanxx.S_LAIHANBH;
                this.txtLaoBH.Text = Convert.ToString(dr["S_LAOBIANH"]);
                this.txtYongHuH.Text = Convert.ToString(dr["S_YONGHUBH"]);
                this.txtYongHuM.Text = Convert.ToString(dr["S_YONGHUM"]);
                this.txtTargetYongHuH.Text = Convert.ToString(dr["S_MUBIAOYHH"]);
                this.txtTargetYongHuM.Text = Convert.ToString(dr["S_MUBIAOYHM"]);
            }
            else if (this.LaiHanLX == eLaiHanLX.水资源销取水口)
            {
                DataRow dr = new SpHelper().P_QSLH_QSKYHXXHQ(laihanxx.S_LAIHANBH, (int)this.LaiHanLX, null).Tables[0].Rows[0];
                this.txtDiZhi.Text = Convert.ToString(dr["S_DIZHI"]);
                this.txtJinBH.Text = Convert.ToString(dr["S_BH"]);
                this.txtLaiHanBH.Text = Convert.ToString(dr["S_LAIHANBH"]);
                this.txtLaoBH.Text = Convert.ToString(dr["S_BHY"]);
                this.txtYongHuH.Text = Convert.ToString(dr["S_YONGHUH"]);
                this.txtYongHuM.Text = Convert.ToString(dr["S_YONGHUM"]);
            }
            else if (this.LaiHanLX == eLaiHanLX.水资源取水口过户)
            {
                DataRow dr = new SpHelper().P_QSLH_QSKYHXXHQ(laihanxx.S_LAIHANBH, (int)this.LaiHanLX, null).Tables[0].Rows[0];
                this.txtDiZhi.Text = Convert.ToString(dr["S_DIZHI"]);
                this.txtJinBH.Text = Convert.ToString(dr["S_BH"]);
                this.txtLaiHanBH.Text = Convert.ToString(dr["S_LAIHANBH"]);
                this.txtLaoBH.Text = Convert.ToString(dr["S_BHY"]);
                this.txtYongHuH.Text = Convert.ToString(dr["S_YONGHUH"]);
                this.txtYongHuM.Text = Convert.ToString(dr["S_YONGHUM"]);
                this.txtTargetYongHuH.Text = Convert.ToString(dr["S_YONGHUHMB"]);
                this.txtTargetYongHuM.Text = Convert.ToString(dr["S_YONGHUMMB"]);
            }
        }

        /// <summary>
        /// 获取来函主表信息
        /// </summary>
        /// <param name="laihanid">ID</param>
        /// <param name="liuchengzt">0:收件 1:收件并登记</param>
        /// <param name="msg">输出消息,正常为空</param>
        /// <returns>null:验证失败 !null:成功返回</returns>
        public YS_LH_LAIHANXX GetLAIHANXX(int laihanid, int liuchengzt, out string msg)
        {
            YS_LH_LAIHANXX laihan = new YS_LH_LAIHANXX();
            string laihanbh = "";
            string xukebh = "";
            msg = "";
            laihan.ID = laihanid;
            if (laihanid == 0)
            {
                string zd, glk, ywy;
                PageTools.GetLHDJZDGLKYWY(this.txtYongHuH.Text, out zd, out glk, out ywy);
                laihan.S_ZHANDIANBH = zd;
                laihan.S_GUANLIKBH = glk;
                laihan.I_YWY = int.Parse(ywy);
                laihan.S_YWY = BusinessTool.GetYWYName(ywy);
                //laihan.S_YHYWY = ywy;
                //DataSet numset = new SpHelper().P_YS_LH_XUKEBH(glk, 1, 0, (BusinessTool.GetWaterYM() / 100).ToString(), out xukebh, out laihanbh);
                new SpHelper().P_ALL_LH_BHSC(BusinessTool.WType, (int)this.LaiHanLX, BusinessTool.GetWaterYM() / 100, out laihanbh);
                laihan.I_XT = BusinessTool.WType;
                laihan.D_SHURURQ = DateTime.Now;
                laihan.D_LAIHANRQ = DateTime.Now;
                laihan.I_JLZT = 0;
                laihan.I_LAIHANLX = (int)this.LaiHanLX;
                laihan.I_SHURUR = LoginInfo.CurrentUser.UserID;
                laihan.I_AUTOTYPE = 0;
                laihan.S_LAIHANBH = laihanbh;
                laihan.S_BEIYONG1 = xukebh;
                laihan.S_XUKEBH = xukebh;
                laihan.I_LIUCHENGZT = (int)eLaiHanLCZT.流程中;
                laihan.I_TZY=laihan.I_Y = BusinessTool.GetWaterYM() / 100;
                laihan.I_TZM=laihan.I_M = BusinessTool.GetWaterYM() % 100;
            }
            else
            {
                laihan = new YS_LH_LAIHANXXProcess().SelectYS_LH_LAIHANXX(laihan.ID);
            }
            laihan.S_BEIZHU = this.txtJinBH.Text;
            laihan.S_YONGHUBH = this.txtYongHuH.Text;
            laihan.S_YONGHUM = this.txtYongHuM.Text;
            laihan.I_LIUCHENGBH = liuchengzt;
            laihan.S_CAOZUOR = LoginInfo.CurrentUser.UserID.ToString();
            laihan.D_CAOZUOSJ = BusinessTool.GetDataBaseDateTime();
            return laihan;
        }

        /// <summary>
        /// 获取来函用户信息
        /// </summary>
        /// <param name="laihan"></param>
        /// <returns></returns>
        public YS_LH_YONGHUXX GetLHYONGHUXX(YS_LH_LAIHANXX laihan)
        {
            return null;
        }

        /// <summary>
        /// 添加申请信息
        /// </summary>
        /// <param name="laihanxx"></param>
        public void CreateShengQingXX(YS_LH_LAIHANXX laihanxx)
        {
            if (this.LaiHanLX == eLaiHanLX.水资源销井)
            {
                //先清除该来函旧的销表水表记录
                QS_LH_XIAOJINSJXXProcess bllT2 = new QS_LH_XIAOJINSJXXProcess();
                bllT2.DeleteQS_LH_XIAOJINSJXXesDynamic("S_LAIHANBH=" + laihanxx.S_LAIHANBH);
                //添加销表水表记录
                QS_LH_XIAOJINSJXX obj = new QS_LH_XIAOJINSJXX();
                obj.S_LAIHANBH = laihanxx.S_LAIHANBH;
                obj.S_JINGBH = this.txtJinBH.Text;
                bllT2.InsertOrUpdate(obj);
            }
            else if (this.LaiHanLX == eLaiHanLX.水资源井过户)
            {
                //先清除该来函旧的销表水表记录
                QS_LH_JINGUOHXXProcess bllT2 = new QS_LH_JINGUOHXXProcess();
                bllT2.DeleteQS_LH_JINGUOHXXesDynamic("S_LAIHANBH=" + laihanxx.S_LAIHANBH);
                QS_LH_JINGUOHXX item = new QS_LH_JINGUOHXX();
                item.S_LAIHANBH = laihanxx.S_LAIHANBH;
                item.S_JINGBH = this.txtJinBH.Text;
                item.S_MUBIAOYHH = this.txtTargetYongHuH.Text;
                item.S_YUANYHH = this.txtYongHuH.Text;
                bllT2.InsertOrUpdate(item);
            }
            else if (this.LaiHanLX == eLaiHanLX.水资源销取水口)
            {
                //先清除该来函旧的销表水表记录
                QS_LH_XIAOQUSHUIKXXProcess bllT2 = new QS_LH_XIAOQUSHUIKXXProcess();
                bllT2.DeleteQS_LH_XIAOQUSHUIKXXesDynamic("S_LAIHANBH=" + laihanxx.S_LAIHANBH);
                QS_LH_XIAOQUSHUIKXX item = new QS_LH_XIAOQUSHUIKXX();
                item.S_LAIHANBH = laihanxx.S_LAIHANBH;
                item.S_BH = this.txtJinBH.Text;
                item.S_BHY = this.txtLaoBH.Text;
                item.S_DIZHI = this.txtDiZhi.Text;
                item.S_YONGHUH = this.txtYongHuH.Text;
                item.S_YONGHUM = this.txtYongHuM.Text;
                bllT2.InsertOrUpdate(item);
            }
            else if (this.LaiHanLX == eLaiHanLX.水资源取水口过户)
            {
                //先清除该来函旧的销表水表记录
                QS_LH_QUSHUIKGHXXProcess bllT2 = new QS_LH_QUSHUIKGHXXProcess();
                bllT2.DeleteQS_LH_QUSHUIKGHXXesDynamic("S_LAIHANBH=" + laihanxx.S_LAIHANBH);
                QS_LH_QUSHUIKGHXX item = new QS_LH_QUSHUIKGHXX();
                item.S_LAIHANBH = laihanxx.S_LAIHANBH;
                item.S_BH = this.txtJinBH.Text;
                item.S_BHY = this.txtLaoBH.Text;
                item.S_DIZHI = this.txtDiZhi.Text;
                item.S_YONGHUH = this.txtYongHuH.Text;
                item.S_YONGHUM = this.txtYongHuM.Text;
                item.S_YONGHUHMB = this.txtTargetYongHuH.Text;
                item.S_YONGHUMMB = this.txtTargetYongHuM.Text;
                bllT2.InsertOrUpdate(item);
            }
        }

        #endregion


        public void Hide()
        {
            this.Visible = false;
        }
    }
}